package com.example.smartcity.ui.dashboard;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smartcity.R;
import com.example.smartcity.ui.Bean.HospitalBannerBean;
import com.example.smartcity.ui.Bean.HospitalMessageBean;
import com.example.smartcity.ui.Tools.File;
import com.example.smartcity.ui.Tools.OkHttpUtils;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class HospitalMessage extends AppCompatActivity {

    private int hospitalId;
    private String TAG = "TAG";
    private Gson gson = null;
    private Banner HospitalBanner;
    private TextView TVMessage;
    private TextView TVOrder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hospital_message);
        HospitalBanner = findViewById(R.id.HospitalBanner);
        TVMessage = findViewById(R.id.TV_Message);
        TVOrder = findViewById(R.id.TV_order);
        initView();
        setTV_order();
    }
    public void setBack(View view){
        this.finish();
    }
    public void initView(){
        Intent intent = getIntent();
        hospitalId = intent.getIntExtra("hospitalId",0);
        loadBanner();
        loadMessage();
    }
    public void loadBanner(){
        String newUrl = OkHttpUtils.getBaseUrl(getBaseContext()) + "/userinfo/img/list?hospitalId=" + hospitalId;
        OkHttpUtils.getCall(newUrl,new File(getBaseContext()).readFile("Token")).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d(TAG, "onFailure: "+e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                gson = new Gson();
                String Json = response.body().string();
                HospitalBannerBean bean =  gson.fromJson(Json, HospitalBannerBean.class);
                if (bean.getCode()==200){
                    List<HospitalBannerBean.RowsBean> list = bean.getRows();
                    HospitalBanner.post(new Runnable() {
                        @Override
                        public void run() {
                            HospitalBanner.setAdapter(new BannerImageAdapter<HospitalBannerBean.RowsBean>(list) {

                                @Override
                                public void onBindView(BannerImageHolder bannerImageHolder, HospitalBannerBean.RowsBean rowsBean, int i, int i1) {
                                    Glide.with(getBaseContext())
                                            .load(OkHttpUtils.getBaseUrl(getBaseContext())+rowsBean.getImgUrl())
                                            .into(bannerImageHolder.imageView);
                                }
                            }).setIndicator(new CircleIndicator(getBaseContext())).start();
                        }
                    });
                }
            }
        });
    }
    public void loadMessage(){
        String newUrl = OkHttpUtils.getBaseUrl(getBaseContext()) + "/userinfo/registration/" + hospitalId;
        OkHttpUtils.getCall(newUrl,new File(getBaseContext()).readFile("Token")).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d(TAG, "onFailure: "+e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                gson = new Gson();
                String Json = response.body().string();
                HospitalMessageBean bean = gson.fromJson(Json,HospitalMessageBean.class);
                if (bean.getCode()==200){
                    HospitalMessageBean.DataBean dataBean= bean.getData();
                    TVMessage.post(new Runnable() {
                        @Override
                        public void run() {
                            TVMessage.setText(dataBean.getBrief());
                        }
                    });
                }
            }
        });
    }
    public void setTV_order(){
        TVOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(HospitalMessage.this.getBaseContext(),addPersionCard.class));
            }
        });
    }
}